Interactive Map

# leaflet map for central park
fur_color <- colorFactor(c("Black","Brown","Gray"),
                               domain = nyc_squirrels_clean$primary_fur_color)

 leaflet() %>% 
  addTiles() %>% 
  fitBounds(-73.9823592,40.7636484,-73.9492441, 40.8005539)%>%
  addTiles() %>% 
  addCircleMarkers(nyc_squirrels_clean$long, 
                   nyc_squirrels_clean$lat, 
                   color = fur_color(nyc_squirrels_clean$primary_fur_color), 
                   radius = 0.5, 
                   fill = T,
                   fillOpacity = 0.2,
                   opacity = 0.6,
                   popup = nyc_squirrels_clean$primary_fur_color)

Table

## Table 1

activity_percentages <- nyc_squirrels_clean %>% 
  select(running, chasing,climbing, eating, foraging, kuks, quaas, moans, tail_flags, tail_twitches, indifferent, runs_from) %>% 
  rownames_to_column(var = "ID") %>% 
  as.data.frame() %>% 
  pivot_longer(cols = running:runs_from, names_to = "activity",
               values_to = "true_false") %>% 
  group_by(activity) %>% 
  nest() %>% 
  mutate(proportion = map(data, ~ .x %>% 
                            pull("true_false") %>% 
                            mean())) %>% 
  unnest(proportion) %>% 
  mutate(percentage = 100*proportion) %>% 
  select(activity, percentage) %>% 
  mutate(activity = gsub("_", " ", activity),
         activity =  str_to_title(activity),
         percentage = round(percentage, 2),
         tooltip_text = paste0(toupper(activity), "\n", 
                   percentage, "%"))

Plot

activity_graph <- activity_percentages %>% 
ggplot(aes(x = percentage, 
           y = reorder(activity, percentage), 
           fill = activity,
           tooltip = tooltip_text, data_id = activity
           )) +
  geom_col_interactive(color = "black", size = 0.3) +
  labs(x = "Percentage", y = "Activity", title = "Activity Percentages") +
  theme_few() 

girafe(ggobj = activity_graph, width_svg = 6, height_svg = 3.5)

HTML Trial

my_widget <- girafe(ggobj = activity_graph, 
                    width_svg = 10, height_svg = 5) %>%
  girafe_options(opts_hover(css = "fill:cyan;"))

htmlwidgets::saveWidget(my_widget, "my_widget_page.html", 
                        selfcontained = TRUE)

Install software

remotes::install_github("hrbrmstr/albersusa")